<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>机台列表展示</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">

    <link rel="stylesheet" href="css/list.css">
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
    <script src="js/jq-paginator.js"></script>

</head>

<body>
    <div class="machineContainer">
        <h2>机台信息总览</h2>
        <div class="navbar-justify-between">
            <div>
                <button class="btn btn-outline-info" type="button" onclick="location.href='machine_add.html'">机台上架</button>
                <button class="btn btn-outline-info" type="button" onclick="batchDelete()">机台批量下架</button>
            </div>
        </div>

        <table>
            <thead>
                <tr>
                    <td>选择</td>
                    <td class="width100">机台ID</td>
                    <td>机台名</td>
                    <td>机台序列号</td>
                    <td>机台位置</td>
                    <td>总测试时长</td>
                    <td>机台测试负责人</td>
                    <td>状态</td>
                    <td class="width200">操作</td>
                </tr>
            </thead>
            <tbody>

            </tbody>
        </table>

        <div class="demo">
            <ul id="pageContainer" class="pagination justify-content-center"></ul>
        </div>
        <script>

            getMachineList();
            function getMachineList() {
                $.ajax({
                        url: "/machine/getMachineListByPage"+location.search,
                        type: "get",
                        success: function (result) {
                            if(result.code =="UNLOGIN"){
                                location.href = "login.html";
                                return;
                            }
                            var finalHtml = "";
                            var pageResult = result.data;
                            for(var machine of pageResult.records){
                                //拼接html
                                finalHtml += '<tr>';
                                finalHtml += '<td><input type="checkbox" name="selectMachine" value="'+ machine.id +'" id="selectMachine" class="machine-select"></td>';
                                finalHtml += '<td>'+machine.id+'</td>';
                                finalHtml += '<td>'+machine.machineName+'</td>';
                                finalHtml += '<td>'+machine.machineSN+'</td>';
                                finalHtml += '<td>'+machine.position+'</td>';
                                finalHtml += '<td>'+machine.testTime+'</td>';
                                finalHtml += '<td>'+machine.publish+'</td>';
                                finalHtml += '<td>'+machine.statusCN+'</td>';
                                finalHtml += '<td><div class="op">';
                                finalHtml += '<a href="machine_update.html?machineId='+machine.id+'">变更</a>';
                                finalHtml += '<a href="javascript:void(0)" onclick="deleteMachine('+machine.id+')">下架</a>';
                                finalHtml += '</div>';
                                finalHtml += '</td>';
                                finalHtml += '<tr>';
                                // finalHtml += "<tr>" ;
                                // finalHtml += "<td><input type=checkbox name=selectMachine id=selectMachine class=machine-select></td>" ;
                                // finalHtml += "<td>"+machine.id+"</td>" ;
                                // finalHtml += "<td>"+machine.machineName+"</td>" ;
                                // finalHtml += "<td>"+machine.machineSN+"</td>" ;
                                // finalHtml += "<td>"+machine.position+"</td>" ;
                                // finalHtml += "<td>"+machine.testTime+"</td>" ;
                                // finalHtml += "<td>"+machine.publish+"</td>" ;
                                // finalHtml += "<td>"+machine.statusCN+"</td>" ;
                                // finalHtml += "<td><div class="+"op"+">" ;
                                // finalHtml += "<a href=machine_update.html?machineId="+machine.id+">修改</a>";
                                // finalHtml += "<a href=javascript:void(0) onclick=deleteMachine("+machine.id+")>删除</a>" ;
                                // finalHtml += "</div>" ;
                                // finalHtml += "</td>" ;
                                // finalHtml += "<tr>" ;
                            }
                            $("tbody").html(finalHtml);

                            //翻页信息
                            $("#pageContainer").jqPaginator({
                                totalCounts: pageResult.total, //总记录数
                                pageSize: 2,    //每页的个数
                                visiblePages: 5, //可视页数
                                currentPage: pageResult.pageRequest.currentPage,  //当前页码
                                first: '<li class="page-item"><a class="page-link">首页</a></li>',
                                prev: '<li class="page-item"><a class="page-link" href="javascript:void(0);">上一页<\/a><\/li>',
                                next: '<li class="page-item"><a class="page-link" href="javascript:void(0);">下一页<\/a><\/li>',
                                last: '<li class="page-item"><a class="page-link" href="javascript:void(0);">最后一页<\/a><\/li>',
                                page: '<li class="page-item"><a class="page-link" href="javascript:void(0);">{{page}}<\/a><\/li>',
                                //页面初始化和页码点击时都会执行
                                onPageChange: function (page, type) {
                                    console.log("第" + page + "页, 类型:" + type);
                                    if(type == "change"){
                                        location.href = "machine_list.html?currentPage="+page;
                                    } 
                                }
                            });
                        },
                        error:function(error){
                            console.log(error);
                            if(error.status == 401){
                                location.href = "login.html"
                            }
                        }
                    });

            }


            function deleteMachine(machineId) {
                var isDelete = confirm("确认下架?");
                if (isDelete) {
                    //删除机台
                    $.ajax({
                        type:"post",
                        url:"/machine/updateMachine",
                        data:{
                            id:machineId,
                            status:0
                        },
                        success:function(result){
                            if(result.code=="SUCCESS" || result.data==""){
                                location.href = "machine_list.html";
                            }else{
                                alert(result);
                            }
                        },
                        error:function(error){
                            if(error!=null && error.status == 401){
                            //用户未登录
                            location.href = "login.html";
                        }
                }
                    });
                }
            }
            function batchDelete() {
                var isDelete = confirm("确认批量删除?");
                if (isDelete) {
                    //获取复选框的id
                    var ids = [];
                    $("input:checkbox[name='selectMachine']:checked").each(function () {
                        ids.push($(this).val());
                    });
                    console.log(ids);
                    $.ajax({
                        type:"post",
                        url:"/machine/batchDelete?ids="+ids,
                        success:function(result){
                            if(result.code=="SUCCESS" || result.data==""){
                                location.href = "machine_list.html";
                            }else{
                                alert(result);
                            }
                        },
                        error:function(error){
                            if(error!=null && error.status == 401){
                            //用户未登录
                            location.href = "login.html";
                            }
                        }
                    });
                }
            }



        </script>
    </div>
</body>

</html>